import request from '@/utils/request'
import type { ApiResponse } from '@/types/api'

// 缓存管理API
export const cacheApi = {
  // 清空权限缓存
  clearPermissionCaches: (): Promise<ApiResponse<any>> => {
    return request.delete('/system/cache/permissions')
  },

  // 清空菜单缓存
  clearMenuCaches: (): Promise<ApiResponse<any>> => {
    return request.delete('/system/cache/menus')
  },

  // 清空字典缓存
  clearDictCaches: (): Promise<ApiResponse<any>> => {
    return request.delete('/system/cache/dicts')
  },

  // 清空所有缓存（慎用）
  clearAllCaches: (): Promise<ApiResponse<any>> => {
    return request.delete('/system/cache/all')
  },

  // 刷新租户权限
  refreshTenantPermissions: (): Promise<ApiResponse<any>> => {
    return request.post('/system/cache/refresh-tenant-permissions')
  },

  // 获取缓存状态
  getCacheStatus: (): Promise<ApiResponse<any>> => {
    return request.get('/system/cache/status')
  },

  // ==================== 缓存监控相关API ====================
  
  // 获取缓存监控总体统计
  getOverallMetrics: (): Promise<ApiResponse<any>> => {
    return request.get('/cache/monitor/metrics/overall')
  },

  // 获取分类型缓存统计
  getCategorizedMetrics: (): Promise<ApiResponse<any>> => {
    return request.get('/cache/monitor/metrics/by-type')
  },

  // 获取热点键统计
  getHotKeys: (limit?: number): Promise<ApiResponse<any>> => {
    return request.get('/cache/monitor/hot-keys', {
      params: { limit: limit || 10 }
    })
  },

  // 获取缓存监控健康状态
  getMonitorHealth: (): Promise<ApiResponse<any>> => {
    return request.get('/cache/monitor/health')
  },

  // 重置缓存监控统计
  resetMonitorMetrics: (): Promise<ApiResponse<any>> => {
    return request.post('/cache/monitor/metrics/reset')
  },

  // 立即刷新缓存监控数据
  refreshMonitorMetrics: (): Promise<ApiResponse<any>> => {
    return request.post('/cache/monitor/metrics/refresh')
  },

  // 获取Redis原生统计
  getRedisNativeStats: (): Promise<ApiResponse<any>> => {
    return request.get('/cache/monitor/redis/native')
  },

  // ==================== 缓存详情管理API ====================
  
  // 获取缓存详情列表（分页）
  getCacheDetails: (cacheType: string, page?: number, size?: number): Promise<ApiResponse<any>> => {
    return request.get(`/cache/detail/list/${cacheType}`, {
      params: { page: page || 1, size: size || 20 }
    })
  },

  // 获取键详细信息
  getKeyInfo: (key: string): Promise<ApiResponse<any>> => {
    return request.get('/cache/detail/key-info', {
      params: { key }
    })
  },

  // 删除缓存键
  deleteKey: (key: string): Promise<ApiResponse<any>> => {
    return request.delete('/cache/detail/key', {
      params: { key }
    })
  },

  // 批量删除缓存键
  deleteKeys: (keys: string[]): Promise<ApiResponse<any>> => {
    return request.delete('/cache/detail/keys', {
      data: keys
    })
  },

  // 设置键过期时间
  expireKey: (key: string, seconds: number): Promise<ApiResponse<any>> => {
    return request.put('/cache/detail/expire', null, {
      params: { key, seconds }
    })
  },

  // 搜索缓存键
  searchKeys: (pattern: string, limit?: number): Promise<ApiResponse<any>> => {
    return request.get('/cache/detail/search', {
      params: { pattern, limit: limit || 100 }
    })
  },

  // 检查键是否存在
  keyExists: (key: string): Promise<ApiResponse<any>> => {
    return request.get('/cache/detail/exists', {
      params: { key }
    })
  },

  // 获取键内存使用
  getKeyMemoryUsage: (key: string): Promise<ApiResponse<any>> => {
    return request.get('/cache/detail/memory-usage', {
      params: { key }
    })
  }
}

export default cacheApi
